/*     */ package com.zimbra.cs.dav.client;
/*     */ 
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.dav.DavContext.Depth;
/*     */ import com.zimbra.cs.dav.DavElements;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.service.UserServlet.HttpInputStream;
/*     */ import java.io.IOException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.httpclient.HttpMethod;
/*     */ import org.dom4j.Document;
/*     */ import org.dom4j.QName;
/*     */ import org.dom4j.io.SAXReader;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CalDavClient
/*     */   extends WebDavClient
/*     */ {
/*     */   public static class Appointment
/*     */   {
/*     */     public String href;
/*     */     public String etag;
/*     */     public String data;
/*     */     public Collection<String> recipients;
/*     */     
/*     */     public Appointment(String h, String e)
/*     */     {
/*  45 */       this.href = h;this.etag = e;
/*     */     }
/*     */     
/*  48 */     public Appointment(String h, String e, String d) { this(h, e);
/*  49 */       this.data = d;
/*     */     }
/*     */     
/*  52 */     public Appointment(String h, String e, String d, Collection<String> r) { this(h, e, d);
/*  53 */       this.recipients = r;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public CalDavClient(String baseUrl)
/*     */   {
/*  62 */     super(baseUrl);
/*  63 */     this.mSchedulingEnabled = false;
/*     */   }
/*     */   
/*     */   public void enableScheduling(boolean enable) {
/*  67 */     this.mSchedulingEnabled = enable;
/*     */   }
/*     */   
/*     */   private String getCurrentUserPrincipal() {
/*  71 */     DavRequest propfind = DavRequest.PROPFIND("/.well-known/caldav");
/*  72 */     propfind.addRequestProp(DavElements.E_CURRENT_USER_PRINCIPAL);
/*  73 */     HttpMethod m = null;
/*     */     try {
/*  75 */       m = executeFollowRedirect(propfind);
/*  76 */       int status = m.getStatusCode();
/*  77 */       if (status >= 400)
/*  78 */         return null;
/*  79 */       doc = com.zimbra.common.soap.Element.getSAXReader().read(m.getResponseBodyAsStream());
/*  80 */       org.dom4j.Element top = doc.getRootElement();
/*  81 */       for (Object obj : top.elements(DavElements.E_RESPONSE))
/*  82 */         if ((obj instanceof org.dom4j.Element)) {
/*  83 */           DavObject davObject = new DavObject((org.dom4j.Element)obj);
/*  84 */           org.dom4j.Element e = davObject.getProperty(DavElements.E_CURRENT_USER_PRINCIPAL);
/*  85 */           if (e != null)
/*  86 */             return e.getStringValue().trim();
/*     */         }
/*     */     } catch (Exception e) {
/*     */       Document doc;
/*  90 */       return null;
/*     */     } finally {
/*  92 */       if (m != null)
/*  93 */         m.releaseConnection();
/*     */     }
/*  95 */     return null;
/*     */   }
/*     */   
/*  98 */   public void login(String defaultPrincipalUrl) throws IOException, DavException { String principalUrl = getCurrentUserPrincipal();
/*  99 */     if (principalUrl == null)
/* 100 */       principalUrl = defaultPrincipalUrl;
/* 101 */     DavRequest propfind = DavRequest.PROPFIND(principalUrl);
/* 102 */     propfind.addRequestProp(DavElements.E_DISPLAYNAME);
/* 103 */     propfind.addRequestProp(DavElements.E_CALENDAR_HOME_SET);
/* 104 */     propfind.addRequestProp(DavElements.E_SCHEDULE_INBOX_URL);
/* 105 */     propfind.addRequestProp(DavElements.E_SCHEDULE_OUTBOX_URL);
/* 106 */     Collection<DavObject> response = sendMultiResponseRequest(propfind);
/* 107 */     if (response.size() != 1)
/* 108 */       throw new DavException("invalid response to propfind on principal url", null);
/* 109 */     DavObject resp = (DavObject)response.iterator().next();
/* 110 */     this.mCalendarHomeSet = new HashSet();
/* 111 */     org.dom4j.Element homeSet = resp.getProperty(DavElements.E_CALENDAR_HOME_SET);
/* 112 */     if (homeSet != null)
/* 113 */       for (Object href : homeSet.elements(DavElements.E_HREF))
/* 114 */         this.mCalendarHomeSet.add(((org.dom4j.Element)href).getText());
/* 115 */     if (this.mCalendarHomeSet.isEmpty())
/* 116 */       throw new DavException("dav response from principal url does not contain calendar-home-set", null);
/* 117 */     org.dom4j.Element elem = resp.getProperty(DavElements.E_SCHEDULE_INBOX_URL);
/* 118 */     if ((elem != null) && (elem.element(DavElements.E_HREF) != null))
/* 119 */       this.mScheduleInbox = elem.element(DavElements.E_HREF).getText();
/* 120 */     elem = resp.getProperty(DavElements.E_SCHEDULE_OUTBOX_URL);
/* 121 */     if ((elem != null) && (elem.element(DavElements.E_HREF) != null))
/* 122 */       this.mScheduleOutbox = elem.element(DavElements.E_HREF).getText();
/*     */   }
/*     */   
/*     */   public Collection<String> getCalendarHomeSet() {
/* 126 */     return this.mCalendarHomeSet;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/* 131 */   private static Collection<QName> CALENDAR_PROPS = new ArrayList();
/* 132 */   static { CALENDAR_PROPS.add(DavElements.E_DISPLAYNAME);
/* 133 */     CALENDAR_PROPS.add(DavElements.E_RESOURCETYPE);
/* 134 */     CALENDAR_PROPS.add(DavElements.E_GETCTAG);
/*     */   }
/*     */   
/* 137 */   public Map<String, DavObject> getCalendars() throws IOException, DavException { HashMap<String, DavObject> calendars = new HashMap();
/* 138 */     for (String calHome : this.mCalendarHomeSet) {
/* 139 */       for (DavObject obj : listObjects(calHome, CALENDAR_PROPS)) {
/* 140 */         String href = obj.getHref();
/* 141 */         String displayName = obj.getDisplayName();
/* 142 */         if ((obj.isCalendarFolder()) && (displayName != null) && (href != null))
/* 143 */           calendars.put(displayName, obj);
/*     */       }
/*     */     }
/* 146 */     return calendars; }
/*     */   
/*     */   private HashSet<String> mCalendarHomeSet;
/*     */   private boolean mSchedulingEnabled;
/* 150 */   public Collection<Appointment> getEtags(String calendarUri) throws IOException, DavException { ArrayList<Appointment> etags = new ArrayList();
/* 151 */     DavRequest propfind = DavRequest.PROPFIND(calendarUri);
/* 152 */     propfind.setDepth(DavContext.Depth.one);
/* 153 */     propfind.addRequestProp(DavElements.E_GETETAG);
/* 154 */     propfind.addRequestProp(DavElements.E_RESOURCETYPE);
/* 155 */     Collection<DavObject> response = sendMultiResponseRequest(propfind);
/* 156 */     for (DavObject obj : response) {
/* 157 */       String href = obj.getHref();
/* 158 */       String etag = obj.getPropertyText(DavElements.E_GETETAG);
/* 159 */       if ((!obj.isFolder()) && (etag != null) && (href != null))
/* 160 */         etags.add(new Appointment(href, etag));
/*     */     }
/* 162 */     return etags;
/*     */   }
/*     */   
/*     */   public Appointment getCalendarData(Appointment appt) throws IOException {
/* 166 */     UserServlet.HttpInputStream resp = sendGet(appt.href);
/* 167 */     appt.data = null;
/* 168 */     if (resp.getStatusCode() == 200) {
/* 169 */       byte[] res = ByteUtil.getContent(resp, resp.getContentLength());
/* 170 */       appt.data = new String(res, "UTF-8");
/*     */     }
/* 172 */     return appt;
/*     */   }
/*     */   
/*     */   public Appointment getEtag(String url) throws IOException, DavException {
/* 176 */     Appointment appt = new Appointment(url, null);
/* 177 */     DavRequest propfind = DavRequest.PROPFIND(url);
/* 178 */     propfind.setDepth(DavContext.Depth.zero);
/* 179 */     propfind.addRequestProp(DavElements.E_GETETAG);
/* 180 */     propfind.addRequestProp(DavElements.E_RESOURCETYPE);
/* 181 */     Collection<DavObject> response = sendMultiResponseRequest(propfind);
/* 182 */     for (DavObject obj : response) {
/* 183 */       String href = obj.getHref();
/* 184 */       if (href.equals(url)) {
/* 185 */         appt.etag = obj.getPropertyText(DavElements.E_GETETAG);
/* 186 */         return appt;
/*     */       }
/*     */     }
/* 189 */     return appt;
/*     */   }
/*     */   
/*     */   public String sendCalendarData(Appointment appt) throws IOException, DavException {
/* 193 */     UserServlet.HttpInputStream resp = sendPut(appt.href, appt.data.getBytes("UTF-8"), "text/calendar", appt.etag, null);
/* 194 */     String etag = resp.getHeader("ETag");
/* 195 */     ZimbraLog.dav.debug("ETags: " + appt.etag + ", " + etag);
/* 196 */     int status = resp.getStatusCode();
/* 197 */     if ((status != 200) && (status != 201) && (status != 204)) {
/* 198 */       throw new DavException("Can't send calendar data (status=" + status + ")", status);
/*     */     }
/* 200 */     if (this.mSchedulingEnabled)
/* 201 */       sendSchedulingMessage(appt);
/* 202 */     return etag;
/*     */   }
/*     */   
/*     */   private String mScheduleInbox;
/*     */   private String mScheduleOutbox;
/*     */   public Collection<Appointment> getCalendarData(String url, Collection<Appointment> hrefs) throws IOException, DavException
/*     */   {
/* 209 */     ArrayList<Appointment> appts = new ArrayList();
/*     */     
/* 211 */     DavRequest multiget = DavRequest.CALENDARMULTIGET(url);
/* 212 */     multiget.addRequestProp(DavElements.E_GETETAG);
/* 213 */     multiget.addRequestProp(DavElements.E_CALENDAR_DATA);
/* 214 */     for (Appointment appt : hrefs)
/* 215 */       multiget.addHref(appt.href);
/* 216 */     Collection<DavObject> response = sendMultiResponseRequest(multiget);
/* 217 */     for (DavObject obj : response) {
/* 218 */       String href = obj.getHref();
/* 219 */       String etag = obj.getPropertyText(DavElements.E_GETETAG);
/* 220 */       String calData = obj.getPropertyText(DavElements.E_CALENDAR_DATA);
/* 221 */       if ((href != null) && (calData != null))
/* 222 */         appts.add(new Appointment(href, etag, calData));
/*     */     }
/* 224 */     return appts;
/*     */   }
/*     */   
/*     */   public Collection<Appointment> getAllCalendarData(String url) throws IOException, DavException {
/* 228 */     ArrayList<Appointment> appts = new ArrayList();
/*     */     
/* 230 */     DavRequest query = DavRequest.CALENDARQUERY(url);
/* 231 */     query.addRequestProp(DavElements.E_GETETAG);
/* 232 */     query.addRequestProp(DavElements.E_CALENDAR_DATA);
/* 233 */     Collection<DavObject> response = sendMultiResponseRequest(query);
/* 234 */     for (DavObject obj : response) {
/* 235 */       String href = obj.getHref();
/* 236 */       String etag = obj.getPropertyText(DavElements.E_GETETAG);
/* 237 */       String calData = obj.getPropertyText(DavElements.E_CALENDAR_DATA);
/* 238 */       if ((href != null) && (calData != null))
/* 239 */         appts.add(new Appointment(href, etag, calData));
/*     */     }
/* 241 */     return appts;
/*     */   }
/*     */   
/*     */   private void sendSchedulingMessage(Appointment appt)
/*     */     throws IOException, DavException
/*     */   {}
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/client/CalDavClient.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */